﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using Cn.Edwon.Data;
using Spring.Context;
using Spring.Context.Support;
using Cn.Cellula.Biz;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Admin.Db.Dm {
    public partial class SQLSelect : PageSupport {
        public static IApplicationContext _context = ContextRegistry.GetContext();
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                InitForm();
            }
        }
        private void InitForm() {
            SetFormByDbType();
        }

        private void LoadBySql() {
            this.lab_Info.Text = "";
            String server = this.txt_Server.Text;
            int port = 0;
            try {
                port = Convert.ToInt32(this.txt_Port.Text);
            } catch {
                this.lab_Info.Text = "错误的端口号，请输入一个整数";
            }
            String user = this.txt_User.Text;
            String pwd = this.txt_Pwd.Text;
            String encode = this.txt_Encode.Text;
            String dbName = this.txt_DbName.Text;

            try {
                if (SiteConfig.System.DbType == CodeTypeDef.DbType.MySql5) {
                    ADO4MySql ado = new ADO4MySql(server, port, dbName, user, pwd, encode);
                    this.gvList.DataSource = ado.CommonSelectAction(this.txt_Sql.Text, "table");
                    this.gvList.DataBind();
                } else if (SiteConfig.System.DbType == CodeTypeDef.DbType.Oracle10g) {
                    ADO4Oracle ado = new ADO4Oracle(server, port, dbName, user, pwd);
                    this.gvList.DataSource = ado.CommonSelectAction(this.txt_Sql.Text, "table");
                    this.gvList.DataBind();
                }
            } catch (Exception ex) {
                this.lab_Info.Text = ex.Message;
                this.gvList.DataSource = new ArrayList();
                this.gvList.DataBind();
            }
        }

        protected void btn_Query_Click(object sender, EventArgs e) {
            LoadBySql();
        }

        protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e) {
            gvList.PageIndex = e.NewPageIndex;
            LoadBySql();
        }

        private void SetFormByDbType() {
            if (SiteConfig.System.DbType == CodeTypeDef.DbType.MySql5) {
                this.txt_Encode.Enabled = true;
                this.txt_Encode.Text = "utf8";
                this.txt_Port.Text = "3306";
            } else if (SiteConfig.System.DbType == CodeTypeDef.DbType.Oracle10g) {
                this.txt_Port.Text = "1521";
                this.txt_Encode.Text = "";
                this.txt_Encode.Enabled = false;
            }
        }

    }
}
